/**
* @Description: WoShop商城
* @Author: 梧桐
* @Copyright: 武汉一一零七科技有限公司©版权所有
* @Link: www.s1107.com
* @Contact: QQ:2487937004
*/
<template>
	<view class="page">
		<view class="back" style="margin-top: 60upx;">
			<image src="@/static/zuo.png" mode=""
				style="width: 50upx;height: 50upx;padding-left: 15upx;padding-top: 10upx;" @click="backk"></image>
		</view>
		<div class="mainBody">

			<view class="header">
				<!-- <u-image class="logoSrc" :src="logoSrc" width="250" height="120" border-radius="15"></u-image> -->
				<h2 class="mainTit">欢迎登录{{AppName}}</h2>
			</view>
			<u-form :model="uForm" ref="uForm" class="u-m-t-30" :error-type="errorType">
				<u-form-item prop="phone" label-position="top">
					<u-input v-model="uForm.phone" placeholder="请输入手机号" />
				</u-form-item>
				<u-form-item prop="password">
					<u-input v-model="uForm.password" placeholder="请输入密码" type="password" minlength="6" />
				</u-form-item>
			</u-form>

			<u-button type="error" class="u-m-t-50" mode="dark" @click="login" :disabled="!isFinishInput">登录</u-button>
			<view class="agreement" @click="changeAgree()">
				<image src="../../static/images/select_active.png" v-if="isSelected"></image>
				<image src="../../static/images/select.png" v-else></image>
				点击按钮表示同意<text @click.stop="navigateToArticle('隐私政策')" style="color: #EC4849;"> 《隐私协议》 </text>
				<text @click.stop="navigateToArticle('用户协议')" style="color: #EC4849;"> 《用户协议》 </text>
			</view>
			<div class="u-flex u-row-between u-m-t-30">
				<view @click="$u.route('/pagesC/register/register')" hover-class="text-hover"
					:style="{color:$u.color['tipsColor']}">立即注册</view>
				<view @click="$u.route('/pagesC/forget/forget')" hover-class="text-hover"
					:style="{color:$u.color['tipsColor']}">忘记密码</view>
			</div>
		</div>


		<!--  #ifdef  APP-PLUS -->
		<view class="other-login u-flex u-row-center" v-if="isWechatShow">

			<view class=""
				style="display: flex;flex-direction: column;justify-content: center;align-items: center;color: #666666;">
				<view style="display: flex;align-items: center;justify-content: center;font-size: 42upx;"><text
						style="width: 100rpx;height: 2upx;background-color: #666;display: inline-block;padding-right: 10upx;"></text>其他登录方式<text
						style="width: 100rpx;height: 2upx;background-color: #666;display: inline-block;padding-left: 10upx;"></text>
				</view>
				<view class=""
					style="display: flex;flex-direction: column;padding-top: 20upx;justify-content: center;align-items: center;">
					<u-icon name="weixin-fill" color="#09D068" size="78" @click="wxLogin"></u-icon>
					<text>微信登录</text>
				</view>

			</view>
		</view>
		<!--  #endif -->

	</view>
</template>

<script>
	import {
		APPNAME
	} from "../../config";

	export default {
		data() {
			return {
				uForm: {
					phone: '',
					password: ''
				},
				isFinishInput: true,
				
				AppName: '',
				errorType: ['none'],
				rules: {
					phone: [{
							required: true,
							message: '请输入手机号',
							trigger: ['change', 'blur'],
						},
						{
							// 自定义验证函数，见上说明
							validator: (rule, value, callback) => {
								// 上面有说，返回true表示校验通过，返回false表示不通过
								// this.$u.test.mobile()就是返回true或者false的
								return this.$u.test.mobile(value);
							},
							message: '手机号码不正确',
							// 触发器可以同时用blur和change
							trigger: ['blur'],
						}
					],
					password: [{
						min: 6,
						message: '请输入不小于6位数的密码'
					}, ]
				},

				phone: '',
				password: '',
				code: '', //验证码		            providerList: [],
				isWechatShow: false,
				isSelected: false, //是否同意协议
				isfalse: true,
				devicetoken: '',
				appInfo: {},
				logoSrc: '',
				cart: 1
			}
		},
		// 必须要在onReady生命周期，因为onLoad生命周期组件可能尚未创建完毕
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		},
		watch: {
			uForm: {
				handler(val) {
					if (val.phone && val.password) {
						this.isFinishInput = true
					} else {
						this.isFinishInput = false
					}
				},
				deep: true
			}
		},
		onLoad(option) {
			this.cart = option.cart
			this.AppName = APPNAME
			this.logoSrc = this.STATICURL + 'images/logo/dsx_logo.png'
			const getPhone = uni.getStorageSync('phone');
			console.log("-> getPhone", getPhone);
			if (getPhone) {
				this.$set(this.uForm, 'phone', getPhone)
			}
			// #ifdef APP-PLUS
			this.getCid();
			// #endif
			this.getThirdLogin();
			this.appInfo = uni.getSystemInfoSync();
			console.log('this', this)
		},

		methods: {
			// 是否同意协议
			changeAgree() {
				this.isSelected = !this.isSelected;
			},
			navigateToArticle(url) {
				if(url == "隐私政策"){
					uni.navigateTo({
						url: `/pagesB/article/detail?tilte=${url}&id=${31}`
					});
				}else{
					uni.navigateTo({
						url: `/pagesB/article/detail?tilte=${url}&id=${42}`
					});
				}
				
			},
			checkResultHandle(e) {
				console.log(e)
				if (e.detail == 'success') {
					this.isfalse = false
				}
			},
			backk() {
				console.log(123)
				uni.switchTab({
					url: '../../pages/index/index'
				})
			},
			getCid() {
				//从缓存中取cid值，如果取到就不调取接口获取

				var info = plus.push.getClientInfo();

				var cid = info.clientid;

				var token = info.token;

				if (cid && token) {
					uni.setStorageSync("devicetoken", cid);
					this.devicetoken = uni.getStorageSync("devicetoken");
				}


			},
			// 登录
			login(e) {
				if (this.uForm.password.length < 6) {
					uni.showToast({
						title: '请输入至少6位数的密码',
						icon: 'none'
					})
				}
				if(this.isSelected == false){
					uni.showToast({
						title:'请先勾选用户协议',
						icon:'none'
					})
					return false
				}
				uni.removeStorageSync('token');
				uni.removeStorageSync('device_token');
				this.$refs.uForm.validate(valid => {
					if (valid) {
						this.$http.getpwddenglu({
							phone: this.uForm.phone,
							password: this.uForm.password,
							devicetoken: this.devicetoken,
						}).then((res) => {
							console.log(res, "登录咯")
							if (res.status == 200) {
								// this.$toast(res.mess)
								uni.showToast({
									title: res.mess,
								})
								uni.setStorageSync('phone', this.uForm.phone);
								console.log("-> this.uForm.phone", this.uForm.phone);
								this.$store.commit('loginStatus', true)
								uni.setStorageSync('token', res.data.token);
								uni.setStorageSync('identity', res.data.identity);
								uni.setStorageSync('role', res.data.role);
								uni.setStorageSync('role_id', res.data.shop_id);
								uni.setStorageSync('pid', res.data.shop_id);
								uni.setStorageSync('serviceShopId', res.data.serviceShopId);

								uni.$emit('accountLogin', {
									isLogin: true
								})
								this.getaddMemberMobile();
								console.log('token' + uni.getStorageSync('token'));
								this.im()
								if (this.cart == 0) {
									uni.switchTab({
										url: "/pages/tabBar/Cart",
									})
								} else {
									this.$u.route({
										url: "/pages/tabBar/mydetail",
										type: "reLaunch"
									})

								}
							} else {
								uni.showToast({
									title: res.mess,
									icon: 'none'
								})
							}

						})
					} else {
						// this.$toast('请正确输入手机号和密码')
						uni.showToast({
							title: res.mess,
							icon: 'none'
						})
					}
				});


			},
			// 判断是否开启第三方登录
			getThirdLogin() {
				this.isWechatShow = true
				this.$http.getThirdLogin().then((res) => {
					if (res.status == 200) {
						this.isWechatShow = true //||res.data.open
					}
				})
			},
			getaddMemberMobile() {
				this.$http.getaddMemberMobile({
					brand: this.appInfo.brand,
					model: this.appInfo.model,
					version: this.appInfo.version,
					system: this.appInfo.system,
					platform: this.appInfo.platform,
				}).then((res) => {
					if (res.status == 200) {
						console.log(this.appInfo);
					}
				})
			},

			// 第三方注册 微信
			wxLogin() {
				if(this.isSelected == false){
					uni.showToast({
						title:'请先勾选用户协议',
						icon:'none'
					})
					return false
				}
				uni.login({
					provider: 'weixin',
					success: (res) => {
						console.log('res:wxLogin ', res);
						uni.getUserInfo({
							provider: 'weixin',
							success: (infoRes) => {
								console.log('infoRes:infoResinfoResinfoResinfoResinfoRes ',
									infoRes);
								this.$loading()
								this.$http.getsfdenglu({
									oauth: 'weixin',
									openid: infoRes.userInfo.openId,
									nick_name: infoRes.userInfo.nickName,
									iconurl: infoRes.userInfo.avatarUrl,
									uniongender: infoRes.userInfo.gender,
									unionId: infoRes.userInfo.unionId,
									devicetoken: this.devicetoken
								}).then(result => {
									if (result.status == 200) {
										this.$toast(result.mess)
										uni.setStorageSync('token', result.data.token);
										uni.setStorageSync('identity', result.data
											.identity);
										uni.setStorageSync('role', result.data
										.role); //角色身份
										uni.setStorageSync('role_id', result.data.shop_id);
										uni.setStorageSync('pid', result.data.shop_id);
										uni.setStorageSync('serviceShopId', result.data
											.serviceShopId);
										this.getaddMemberMobile();
										setTimeout(() => {
											this.im()
											uni.switchTab({
												url: '/pages/tabBar/mydetail'
											})
										}, 1500);
									} else {
										console.log(JSON.stringify(result))
									}

								}).catch(err => {
									console.log(JSON.stringify(err));

								})

							}
						});

					},
					fail: (err) => {
						console.log('login fail:', err);
					}
				});
			},
			// 获取手机信息

			navigateTo(url, data) {
				this.$navigateTo(url)
			},
			goHome() {
				uni.switchTab({
					url: '/pages/index/index',
				})
			},

		}
	}
</script>

<style lang="scss" scoped>
	@import "~uview-ui/index.scss";

	.agreement {
		font-size: 28upx;
		color: #999;
		margin-top: 36upx;
		padding-left: 8upx;
		padding-bottom: 20upx;
		display: flex;
		align-items: center;

		image {
			margin-right: 4upx;
			width: 25upx;
			height: 25upx;
		}

	}

	page {
		background: #ffffff;

	}

	.mainBody {
		padding: 80upx;
		margin-top: 140upx;

		.mainTit {
			margin: 30upx 0 10upx;
			font-size: 48upx;
		}

		.subTit {
			font-weight: lighter;
		}
	}

	.logoSrc {
		width: 110upx;
		height: 120upx;
		border-radius: 200px;
		position: relative;
		// box-shadow: 0 0 0 6upx #ffd0d0,0 0 0 14upx #ffefef;
	}

	/deep/uni-view {
		line-height: 2.4;
	}
</style>
